package com.coocaa.app.lib.cleaner.deleter.pkgdata.impl;

/**
 * Created by lu on 17-4-13.
 * <p>
 * public class PackageDataDeleterImpl implements PackageDataDeleterFactory.PackageDataDeleter {
 * private int count = 0;
 *
 * @Override public void delete(final Context context, final DataHolder.SinglePackageData data, final PackageDataDeleterListener listener) {
 * new Thread(new Runnable() {
 * @Override public void run() {
 * final List<String> list = new ArrayList<String>();
 * list.add(data.packageName);
 * listener.onPackageDataDeleterStart(false, list);
 * <p>
 * CleanUtils cu = new CleanUtils(context);
 * <p>
 * if(cu.needSpecialClean(data.packageName)){
 * try {
 * cu.deleteSpecialApplicationData(data.packageName, new CleanUtils.SpecialPackageDataClearListener() {
 * @Override public void onDeleteDataComplete(String packageName, boolean succeeded, long size) {
 * listener.onPackageDataDeleterEnd(false, list, succeeded, size);
 * }
 * });
 * }catch (Exception e){
 * listener.onPackageDataDeleterEnd(false, list, false, data.size);
 * e.printStackTrace();
 * }
 * }else {
 * try {
 * cu.deleteApplicationUserData(data.packageName, new CleanUtils.PackageDataClearListener() {
 * @Override public void onDeleteDataComplete(String packageName, boolean succeeded, String errorMsg) {
 * listener.onPackageDataDeleterEnd(false, list, succeeded, data.size);
 * }
 * });
 * } catch (Exception e) {
 * listener.onPackageDataDeleterEnd(false, list, false, data.size);
 * e.printStackTrace();
 * }
 * }
 * }
 * }).start();
 * }
 * @Override public void delete(final Context context, final List<DataHolder.SinglePackageData> datas, final PackageDataDeleterListener listener) {
 * new Thread(new Runnable() {
 * @Override public void run() {
 * count = datas.size();
 * List<String> list = new ArrayList<String>();
 * final List<DataHolder.SinglePackageData> resData = new ArrayList<DataHolder.SinglePackageData>();
 * listener.onPackageDataDeleterStart(true, list);
 * for (final DataHolder.SinglePackageData data : datas) {
 * CleanUtils cu = new CleanUtils(context);
 * if(cu.needSpecialClean(data.packageName)){
 * try {
 * cu.deleteSpecialApplicationData(data.packageName, new CleanUtils.SpecialPackageDataClearListener() {
 * @Override public void onDeleteDataComplete(String packageName, boolean succeeded, long size) {
 * //                                    listener.onPackageDataDeleterEnd(false, list, succeeded, size);
 * if (succeeded)
 * resData.add(data);
 * synchronized (resData) {
 * count -= 1;
 * if (count == 0) {
 * resData.notifyAll();
 * }
 * }
 * }
 * });
 * }catch (Exception e){
 * e.printStackTrace();
 * }
 * }else {
 * try {
 * cu.deleteApplicationUserData(data.packageName, new CleanUtils.PackageDataClearListener() {
 * @Override public void onDeleteDataComplete(String packageName, boolean succeeded, String errorMsg) {
 * if (succeeded)
 * resData.add(data);
 * synchronized (resData) {
 * count -= 1;
 * if (count == 0) {
 * resData.notifyAll();
 * }
 * }
 * }
 * });
 * } catch (Exception e) {
 * e.printStackTrace();
 * }
 * }
 * }
 * <p>
 * synchronized (resData) {
 * if (count > 0)
 * try {
 * resData.wait();
 * } catch (InterruptedException e) {
 * e.printStackTrace();
 * }
 * }
 * <p>
 * long size = 0;
 * if (resData.size() > 0) {
 * for (DataHolder.SinglePackageData data : resData) {
 * size += data.size;
 * list.add(data.packageName);
 * }
 * }
 * if (size > 0) {
 * listener.onPackageDataDeleterEnd(true, list, true, size);
 * } else {
 * listener.onPackageDataDeleterEnd(true, list, false, 0);
 * }
 * }
 * }).start();
 * }
 * }
 */
/**
 public class PackageDataDeleterImpl implements PackageDataDeleterFactory.PackageDataDeleter {
 private int count = 0;

 @Override public void delete(final Context context, final DataHolder.SinglePackageData data, final PackageDataDeleterListener listener) {
 new Thread(new Runnable() {
 @Override public void run() {
 final List<String> list = new ArrayList<String>();
 list.add(data.packageName);
 listener.onPackageDataDeleterStart(false, list);

 CleanUtils cu = new CleanUtils(context);

 if(cu.needSpecialClean(data.packageName)){
 try {
 cu.deleteSpecialApplicationData(data.packageName, new CleanUtils.SpecialPackageDataClearListener() {
 @Override public void onDeleteDataComplete(String packageName, boolean succeeded, long size) {
 listener.onPackageDataDeleterEnd(false, list, succeeded, size);
 }
 });
 }catch (Exception e){
 listener.onPackageDataDeleterEnd(false, list, false, data.size);
 e.printStackTrace();
 }
 }else {
 try {
 cu.deleteApplicationUserData(data.packageName, new CleanUtils.PackageDataClearListener() {
 @Override public void onDeleteDataComplete(String packageName, boolean succeeded, String errorMsg) {
 listener.onPackageDataDeleterEnd(false, list, succeeded, data.size);
 }
 });
 } catch (Exception e) {
 listener.onPackageDataDeleterEnd(false, list, false, data.size);
 e.printStackTrace();
 }
 }
 }
 }).start();
 }

 @Override public void delete(final Context context, final List<DataHolder.SinglePackageData> datas, final PackageDataDeleterListener listener) {
 new Thread(new Runnable() {
 @Override public void run() {
 count = datas.size();
 List<String> list = new ArrayList<String>();
 final List<DataHolder.SinglePackageData> resData = new ArrayList<DataHolder.SinglePackageData>();
 listener.onPackageDataDeleterStart(true, list);
 for (final DataHolder.SinglePackageData data : datas) {
 CleanUtils cu = new CleanUtils(context);
 if(cu.needSpecialClean(data.packageName)){
 try {
 cu.deleteSpecialApplicationData(data.packageName, new CleanUtils.SpecialPackageDataClearListener() {
 @Override public void onDeleteDataComplete(String packageName, boolean succeeded, long size) {
 //                                    listener.onPackageDataDeleterEnd(false, list, succeeded, size);
 if (succeeded)
 resData.add(data);
 synchronized (resData) {
 count -= 1;
 if (count == 0) {
 resData.notifyAll();
 }
 }
 }
 });
 }catch (Exception e){
 e.printStackTrace();
 }
 }else {
 try {
 cu.deleteApplicationUserData(data.packageName, new CleanUtils.PackageDataClearListener() {
 @Override public void onDeleteDataComplete(String packageName, boolean succeeded, String errorMsg) {
 if (succeeded)
 resData.add(data);
 synchronized (resData) {
 count -= 1;
 if (count == 0) {
 resData.notifyAll();
 }
 }
 }
 });
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 }

 synchronized (resData) {
 if (count > 0)
 try {
 resData.wait();
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }

 long size = 0;
 if (resData.size() > 0) {
 for (DataHolder.SinglePackageData data : resData) {
 size += data.size;
 list.add(data.packageName);
 }
 }
 if (size > 0) {
 listener.onPackageDataDeleterEnd(true, list, true, size);
 } else {
 listener.onPackageDataDeleterEnd(true, list, false, 0);
 }
 }
 }).start();
 }
 }*/